﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Repository;
using ZR.Service.IService.ProductionControl;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Model.Dto;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 三坐标数据追溯服务
    /// </summary>
    [AppService(ServiceType = typeof(ICksCMMService), ServiceLifetime = LifeTime.Transient)]
    public class CksCMMService : BaseService<CksCMMInspectionRecord>, ICksCMMService
    {
        public CksCMMService()
        { }

        public PagedInfo<CksCMMDetailDto> GetList(CksCMMDetailQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .OrderByDescending(x => x.CreateTime)
                .Where(predicate.ToExpression())
                .ToPage<CksCMMInspectionRecord, CksCMMDetailDto>(parm);

            return response;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksCMMInspectionRecord> QueryExp(CksCMMDetailQueryDto parm)
        {
            var predicate = Expressionable.Create<CksCMMInspectionRecord>();
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.ProductId), it => it.ProductId.Contains(parm.ProductId));
            predicate = predicate.AndIF(parm.Result.IsNotEmpty(), it => it.CheckResult == ((bool)parm.Result ? "OK" : "NG"));
            predicate = predicate.AndIF(parm.BeginAddTime != null, it => it.CreateTime >= parm.BeginAddTime);
            predicate = predicate.AndIF(parm.EndAddTime != null, it => it.CreateTime <= parm.EndAddTime);
            return predicate;
        }

        public CksCMMInspectionRecord GetInfo(long id)
        {
           return GetById(id);
        }
    }
}